<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ include file="/WEB-INF/common/common.jsp"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <meta charset="utf-8">
  <title>layui</title>
  <meta name="renderer" content="webkit">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <link rel="stylesheet" href="<%=path %>/layui/css/layui.css">
  <link rel="stylesheet" href="<%=path %>/layui/css/lxq.css">
	<script type="text/javascript" src="<%=path %>/layui/jquery-3.3.1.min.js"></script>
    <script type="text/javascript" src="<%=path %>/layui/layui.js" charset="utf-8"></script>
    <script type="text/javascript" src="<%=path %>/layui/util.js" charset="utf-8"></script>
</head>
<body>
<div class="lxq-body">
	<div class="layui-row lxq-address">
		位置：收集表管理>添加填写权限
		<hr>
	</div>

  <div class="layui-row layui-col-space10">
  	 <form class="layui-form"  method="post" id="searchForm">
	<div class="layui-row lxq-search" >
		<div class="data-search">
		  搜索ID：
		  <div class="layui-inline">
		    <input class="layui-input" name="userId" id="userId" autocomplete="off">
		  </div>
		  学院：
		  <div class="layui-inline">
		     <select id="userAcademy" name="userAcademyId" lay-filter="userAcademy" lay-search></select>
		  </div>
		   专业：
		  <div class="layui-inline">
		    <select id="userMajor" name="userMajorId" lay-filter="userMajor" lay-search></select>
		  </div>
		  班级：
		  <div class="layui-inline">
		     <select id="userClass" name="userClassId" lay-filter="userClass" lay-search></select>
		  </div>
		  部门：
		  <div class="layui-inline">
		     <select id="userUnit" name="userUnitId" lay-filter="userUnit" lay-search></select>
		  </div>
		  <button class="layui-btn" lay-submit lay-filter="search">搜索</button>
		  <button type="reset" class="layui-btn layui-btn-primary">重置</button>
		</div>
	</div>
</form>
  
    <div class="layui-col-md9">
	 	<div class="lxq-form">
	 		<table class="layui-hide" id="userTable" lay-filter="userTable"></table> 
	 	</div>
    </div>
    <div class="layui-col-md3">
	 	<div class="lxq-form">
	 		<table class="layui-hide" id="writerTable" lay-filter="writerTable"></table> 
	 	</div>
    </div>
  </div>

	


</div>              
<script>
layui.use(['table'], function(){
  var table = layui.table;
  var index = parent.layer.getFrameIndex(window.name);
  
  //加载用户表格
  table.render({
	    elem: '#userTable'
	    ,url:'<%=path %>/biz/SysUser_list'
	    ,toolbar: '#toolbarUser'
		,height: 'full-20'
		,limit: 1000
	    ,cols: [[
		   {type: 'checkbox', fixed: 'left'}
	      ,{field:'userId',  title: 'ID', sort: true}
	      ,{field:'userName',  title: '姓名'}
	      ,{field:'userType', title: '用户类型', templet:'<div>{{ Situation(d.userType,"0,1,9","学生,教师,管理员")}}</div>'}
	      ,{field:'userAcademy', title: '学院'}
	      ,{field:'userMajor', title: '专业'}
	      ,{field:'userClass', title: '班级'}
	      ,{field:'userUnit', title: '部门'}
	      ,{fixed: 'right', title:'操作', align:'center', toolbar: '#barUser'}
	    ]]
 		 ,id: 'userTable'
	  });
  //加载数据表格
    table.render({
	    elem: '#writerTable'
	  //  ,url:'<%=basePath %>/biz/Main_getWriter?id='+${form.id}
	  	,data: []
	    ,toolbar: '#toolbarWriter'
	    ,height: 'full-20'
	    ,limit: 1000
	    ,cols: [[
	      {field:'userId',  title: 'ID', sort: true}
	      ,{field:'userName',  title: '姓名'}
	      ,{fixed: 'right', title:'操作', align:'center', toolbar: '#barWriter'}
	    ]]
 		 ,id: 'writerTable'
	  });
  

  
	  //监听工具条
	  table.on('tool(userTable)', function(obj){
	    var data = obj.data;
	    if(obj.event === 'add'){
	         var oldData =  table.cache["writerTable"];
	          var data1=data;
	          oldData.push(data1);
	          table.reload('writerTable',{
	              data : oldData
	          });
	          //obj.del();
	    }
	  });
	  //头工具栏事件
	  table.on('toolbar(userTable)', function(obj){
	    var checkStatus = table.checkStatus(obj.config.id);
	    switch(obj.event){
	      case 'add':
	          var dataFriend = checkStatus.data;
	          var json = JSON.stringify(dataFriend);
		      var oldData =  table.cache["writerTable"];
		      //var newData = Array.from(new Set(dataFriend.concat(oldData)));
		      var newData = unique(dataFriend,oldData);
	          table.reload('writerTable',{
	              data : newData
	          });
	      break;

	    };
	  });
	  
	  //监听工具条
	  table.on('tool(writerTable)', function(obj){
	    var data = obj.data;
	    if(obj.event === 'del'){
	          obj.del();
	    }
	  });
	  //头工具栏事件
	  table.on('toolbar(writerTable)', function(obj){
	    switch(obj.event){
	      case 'add':
	    	  
	          var json = JSON.stringify(table.cache["writerTable"]);
		    	var formid = "${form.formId}";
			  	  $.ajax({
						type:"post",
						url:"<%=basePath%>/biz/Main_addWriter",
						data:{json : json, formId : formid},
						success: function(data){
							parent.layer.close(index);
						}
				  });
	      break;
	      case 'getBranch':
	    	  //获得联系人
	    	  	$.ajax({
					type:"post",
					url:"<%=basePath%>/biz/Branch_getFriend",
					success: function(data){
						var oldData =  table.cache["writerTable"];
						var newData = unique(data.data,oldData);
				          table.reload('writerTable',{
				              data : newData
				          });
					}
				});
	      break;

	    };
	  });
	  
	  
	  
  
  var active = {
		    reload: function(){
		      var searchId = $('#search_id');
		      table.reload("data_form",{
		    	  url:'<%=path %>/biz/SysUser_list'
		    	  ,where: {
		    		  userId: searchId.val()
		    	  }
		          ,page: {
		            curr: 1 //重新从第 1 页开始
		          }
		      });
		      //执行重载
		    }
  
		  };
		  
		  $('.data-search .layui-btn').on('click', function(){
		    var type = $(this).data('type');
		    active[type] ? active[type].call(this) : '';
		  });

  
});
</script>

<script>
function unique(newData, oldData){ 
	for (var i in newData){
	    var flag = 0;
		for (var j in oldData){
		    if(newData[i].userId == oldData[j].userId){
		    	flag = 1;
		    }
		}
		
		if(flag == 0){
			
			oldData.push(newData[i]);
		}
	}
	return oldData;
}
</script>


    <script type="text/html" id="barUser">
  <a class="layui-btn layui-btn-xs" lay-event="add">添加</a>
	</script>
	
	<script type="text/html" id="barWriter">
  <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
	</script>

<script type="text/html" id="toolbarUser">
  <div class="layui-btn-container">
    <button class="layui-btn layui-btn-sm" lay-event="add">添加</button>
  </div>
</script>

<script type="text/html" id="toolbarWriter">
  <div class="layui-btn-container">
    <button class="layui-btn layui-btn-sm" lay-event="getBranch">加入联系人</button>
<button class="layui-btn layui-btn-sm" lay-event="add">完成</button>
  </div>
</script>
</body>
</html>